#!/usr/bin/python

import twitter
from datetime import datetime

from SleepyTracker import SleepyTracker
import sys

def conv_hour(hour):
        if hour <= 12:
            return str(hour)+"AM"
        else:
            return str(hour-12)+"PM"
    

if len(sys.argv) != 2:
    print "Usage: extract <device path>"
    sys.exit(1) 

tracker = SleepyTracker(sys.argv[1])
data = tracker.data
# Add your username/password here!
api = twitter.Api(username='yourusername', password='yourpassword')

if data.wake_up_time < data.alarm_time:
    api.PostDirectMessage('daytum','EARLY')
else:
    api.PostDirectMessage('daytum','ALARM')

# Work out how long your were asleep

sleeptime = None
td = datetime.today()
d1 = datetime(td.year, td.month, td.day, data.wake_up_time.hour, data.wake_up_time.minute, data.wake_up_time.second)
d2 = None

if data.to_bed.hour < data.wake_up_time.hour:
    # Woke up the same day you went to bed
    d2 = datetime(td.year, td.month, td.day, data.to_bed.hour, data.to_bed.minute, data.to_bed.second)
else:
    # Woke up the next day 
    d2 = datetime(td.year, td.month, td.day-1, data.to_bed.hour, data.to_bed.minute, data.to_bed.second)

sleeptime = d1 - d2
hours = int(round(sleeptime.seconds / 60 / 60))
api.PostDirectMessage('daytum',conv_hour(data.to_bed.hour))
api.PostDirectMessage('daytum',conv_hour(data.wake_up_time.hour))
api.PostDirectMessage('daytum', 'SLEEPTIME : '+str(hours))
api.PostDirectMessage('daytum', 'QUALITY : '+str(round(data.awake_data_a.seconds / 60)))
